package com.java1234.web;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import com.java1234.dao.MessageDao;
import com.java1234.model.Book;
import com.java1234.model.Category;
import com.java1234.model.Message;
import com.java1234.model.Student;
import com.java1234.util.DbUtil;
import com.java1234.util.ExportExcelUtils;


public class ExportExcelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	  DbUtil dbUtil=new DbUtil();
	  MessageDao messageDao=new MessageDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String delIds=request.getParameter("delIds");		
		String Sid=request.getParameter("id");
		int id = Integer.parseInt(Sid);
		//导出查询	
		//String s_Name=request.getParameter("Name");
		String Name = new String(request.getParameter("Name").getBytes("ISO-8859-1"),"utf-8");
		String head = new String(request.getParameter("head").getBytes("ISO-8859-1"),"utf-8");
		String workspace = new String(request.getParameter("workspace").getBytes("ISO-8859-1"),"utf-8");
		String aid = new String(request.getParameter("aid").getBytes("ISO-8859-1"),"utf-8");
		String members = new String(request.getParameter("members").getBytes("ISO-8859-1"),"utf-8");
		if(Name==null){
			Name="";
		}
		if(head==null){
			head="";
		}
		if(workspace==null){
			workspace="";
		}
		if(aid==null){
			aid="";
		}
		if(members==null){
			members="";
		}
				Message message=new Message();
				message.setName(Name);
				message.setHead(head);
				message.setWorkspace(workspace);
				message.setAid(aid);
				message.setMembers(members);
				//已立项搜索数据导出
				if(id==9){
					message.setIs_confirm("已立项");
				}
				if(id==10){
					message.setSubmit("已提交");
				}
				if(id==11){
					message.setIs_end("已结项");
				}
				if(id==12){
					message.setPrize("未获奖");
				}
	      Connection con= null;
			try {
				con = dbUtil.getCon();
			} catch (Exception e1) {
				
				e1.printStackTrace();
			}
			
			List<Message> exportMessage = null;
			try {
				if(id==1){
					exportMessage= messageDao.exportAllPrizeMessage(con);
				}else if(id==2){
					exportMessage= messageDao.exportAllEndMessage(con);
				}else if(id==3){
					exportMessage= messageDao.exportAllConfirmMessage(con);
				}else if(id==5){
					exportMessage= messageDao.exportAllPrizeOMessage(con);
				}else if(id==6){
					exportMessage= messageDao.exportAllPrizeTMessage(con);
				}else if(id==7){
					exportMessage= messageDao.exportAllPrizeFMessage(con);
				}else if(id==8||id==9||id==10||id==11||id==12){
					exportMessage= messageDao.exportSearchMessage(con,message);	
				}else if(id==4){
					exportMessage= messageDao.exportAllMessage(con);
				}else{
					exportMessage= messageDao.exportMessage(con, delIds);
				}
					
				
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			response.setContentType("octets/stream");
		    response.addHeader("Content-Disposition", "attachment;filename=import.xls");
			
	      // 测试信息
	      ExportExcelUtils<Message> ex = new ExportExcelUtils<Message>();
	      String[] headers = {"序号", "年份", "分类", "选题方向", "项目名称", "项目形式", "项目负责人", "职称", "工作单位", "联系方式","项目组成员", "备注", "是否立项","是否结项","获奖情况" };
	      List<Message> dataset = new ArrayList<Message>();
	  
	    	  for(int i = 0;i<exportMessage.size(); i++){
		    	  dataset.add(exportMessage.get(i));
		      } 
	      
	      Collection datesetCo=dataset;
	      
	      OutputStream out = response.getOutputStream();
	         ex.exportExcel(headers, datesetCo, out);
	         out.close();
	 
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
